Process Analysis Toolkit  (PAT) 3.5 Help  
2.5.1 External Static Methods

Some complicated calculations (e.g., algorithms, data operations and computations) are difficult to implement in PAT's modeling language. It is easier to write them using programming language like C#. We provide user such option to implement the calculations using C# static methods and invoke these methods in PAT.

The following is one simple example of showing how to write static methods in C#.

using System.Collections.Generic;
using PAT.Common.Classes.Expressions.ExpressionClass;

// the namespace must be PAT.Lib, the class and method names can be arbitrary
namespace PAT.Lib {
    public class PatList {
        public static int[] ListAdd(int[] list, int element) {
            List<int> newList = new List<int>(list);
            newList.Add(element);
            return newList.ToArray();
        }
       
        public static bool ListContains(int[] list, int element) {
            foreach (int i in list) {
                if (i == element) {
                    return true;
                }
            }
            return false;           
        }
    } 
}

Note the following requirements when you create your own libraries:

  • The namespace must be "PAT.Lib", otherwise it will not be recognized. There is no restriction for class names and method names.
  • Importing the PAT Expression name space using "using PAT.Common.Classes.Expressions.ExpressionClass;".
  • All methods should be declared as public static. You can also use (private) static variables and functions to support your methods.
  • The parameters must be of type "bool", "int", "int[]" (int array) or object (object type allows user to pass user defined data type as parameter).
  • The number of parameters can be 0 or many.
  • The return type must be of type "void", "bool", "int", "short", "byte", "int[]" (int array) or user defined data type.
  • The method names are case-sensitive.
  • Put the compiled DLLs in "Lib" folder of the PAT installation directory to make the linking easy by using #import "DLL_Name";  or you can put the DLLs under same folder as the model. If the DLLs are put in other places, you need to put the full path of the DLLs after import keyword. 
  • To import math library, please use #import "PAT.Math";

If your methods need to handle exceptional cases, you can throw PAT runtime exceptions as illustrated as the following example.

public static int StackPeek(int[] array) {
    if (array.Length > 0)
           return array[array.Length - 1];

 //throw PAT Runtime exception
    throw new PAT.Common.Classes.Expressions.ExpressionClass.RuntimeException("Access an empty stack!");
}

To import the libraries in your model, users can using following syntax:

  • #import "PAT.Lib.Set"; //to import a library under Lib folder of PAT installation path
  • #import "C:\Program Files\Intel\Set.dll"; //to import a library using absolute path

  • x = call(Max, 10, 2);
  • if(call(dominate, 3, 2))...
  • y = call(ArrayMax, [1,3,5]);

Note: the method names are case sensitive.

The build-in C# math functions you can use are listed as follows:

  • int Abs(int d)
  • int BigMul(int a, int b)
  • int Exp(int d)
  • int Max(int val1, int val2)
  • int Min(int val1, int val2)
  • int Pow(int val1, int val2)
  • int Sqrt(int d)
  • int Round(int a)

 
Copyright © 2007-2012 Semantic Engineering Pte. Ltd.